package com.example.springboot.model;

import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class MessageHandler {
    private static final ExecutorService executor = Executors.newCachedThreadPool();
    private static final ApiService apiService = new ApiService();

    public void handleMessage(String text, Session session) {
        executor.submit(() -> {
            try {
                apiService.sendRequest(text, session);
            } catch (Exception e) {
                log.error("Error handling message", e);
                try {
                    session.getBasicRemote().sendText("Error: " + e.getMessage());
                } catch (IOException e1) {
                    log.error("Failed to send error message", e1);
                }
            }
        });
    }
}
